﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using Microsoft.Win32;

namespace csvformater.csvWriter
{
    class NzCsvWriter
    {
        /*
         * Asks a filename and location from the user to export the data, and
         * runs the export operation.
         */
        public void ExportToCsv(DataTable dataTable, string separator)
        {

            //Asks the filenam with a SaveFileDialog control.

            var saveFileDialogCsv = new SaveFileDialog();
            saveFileDialogCsv.InitialDirectory = Environment.CurrentDirectory;
            saveFileDialogCsv.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
            saveFileDialogCsv.FilterIndex = 1;
            saveFileDialogCsv.RestoreDirectory = true;

            if ((bool)saveFileDialogCsv.ShowDialog())
            {
                // Runs the export operation if the given filenam is valid.
                exportToCSVfile(saveFileDialogCsv.FileName, dataTable, separator);
            }
        }
        private string columnNames(DataTable dtSchemaTable, string delimiter)
        {
            string strOut = "";
            if (delimiter.ToLower() == "tab")
            {
                delimiter = "\t";
            }

            for (int i = 0; i < dtSchemaTable.Columns.Count; i++)
            {
                strOut += dtSchemaTable.Columns[i].ColumnName;
                if (i < dtSchemaTable.Columns.Count - 1)
                {
                    strOut += delimiter;
                }

            }
            return strOut;
        }
        private void exportToCSVfile(string fileOut, DataTable dataTable, string separator)
        {

            // Creates the CSV file as a stream, using the given encoding.
            StreamWriter sw = new StreamWriter(fileOut, false, Encoding.Default);

            string strRow; // represents a full row

            // Writes the column headers if the user previously asked that.
            sw.WriteLine(columnNames(dataTable,separator));

            // Reads the rows one by one from the SqlDataReader
            // transfers them to a string with the given separator character and
            // writes it to the file.
            for (int rowIndex = 0; rowIndex < dataTable.Rows.Count; rowIndex++)
            {
                strRow = "";
                for (int i = 0; i < dataTable.Columns.Count; i++)
                {
                    strRow += dataTable.Rows[rowIndex][i].ToString();
                    if (i < dataTable.Columns.Count - 1)
                    {
                        strRow += separator;
                    }
                }
                sw.WriteLine(strRow);

            }

            // Closes the text stream and the database connenction.
            sw.Close();

            // Notifies the user.
            MessageBox.Show("已经成功导出文件");
        }
    }
}
